from rest_framework.views import exception_handler

from .response import APIResponse
from .logger import log


def common_exception_handler(exc, context):
    # print(exc, context)
    log.error('view是:%s, 错误是:%s' % (context['view'].__class__.__name__, str(exc)))  # context['view']是错误的页面
    ret = exception_handler(exc, context)

    if not ret:  # drf内置处理不了，丢给django的，在这里处理
        if isinstance(exc, ZeroDivisionError):
            print('1/0')
            return APIResponse(code=0, msg='1/0错误', result=str(exc))
        return APIResponse(code=0, msg=str(exc))
    else:
        return APIResponse(code=0, msg='error', result=ret.data)
